package com.wang.di.defalut.anno_executor;

import com.alibaba.fastjson.JSONObject;
import com.wang.di.core.handler.AnnoHandler;
import com.wang.di.defalut.anno.Value;
import com.wang.utils.IOUtils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;

public class ValueExecutor extends AnnoHandler<Value> {
    private JSONObject JSON_MAP = null;

    private ValueExecutor() {
    }

    public static ValueExecutor init(File configFile) throws IOException {
        String txt = IOUtils.readAll(configFile);
        ValueExecutor valueExecutor = new ValueExecutor();
        valueExecutor.JSON_MAP = JSONObject.parseObject(txt);
        return valueExecutor;
    }

    @Override
    public Object field(Object instance, Class<?> clz, Field field, Value anno) throws Exception {
        String key = anno.name();
        return JSON_MAP.get(key);
    }
}
